<template>

  <el-dialog
    title="修改密码"
    :visible="updatePasswordShow"
    :show-close="showClose"
    :destroy-on-close="destroyOnClose"
    width="1100px">

    <el-form ref="formPassword" :rules="rulesPassword" :model="formPassword" label-width="80px">
      <el-form-item label="旧密码" prop="old" >
        <el-input type="password" v-model="formPassword.old"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="news">
        <el-input type="password" v-model="formPassword.news"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="newsTwo">
        <el-input type="password" v-model="formPassword.newsTwo"></el-input>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="updatePasswordShow = false">取消修改</el-button>
      <el-button type="primary" @click="updPassword">修改密码</el-button>
    </span>

  </el-dialog>

</template>

<script>

  import http from '@/utils/http';
  import {desEncrypt} from "../../utils/utils";

  export default {
    name: "UpdatePassword",
    data(){
      return {
        // 修改密码弹出框是否打开
        updatePasswordShow: false,
        // 关闭时销毁 Dialog 中的元素
        destroyOnClose: true,
        // 是否显示关闭按钮
        showClose: true,
        // 要提交的密码
        formPassword: {
          // 旧密码
          old: null,
          // 新密码
          news: null,
          // 新密码2
          newsTwo: null,
        },
        // 校验规则
        rulesPassword: {
          old: [
            { required: true, message: '请输入旧密码', trigger: 'blur' }
          ],
          news: [
            // 调用校验规则
            { required: true, message: '请输入新密码', trigger: 'blur' },
            { min: 5, max: 30, message: '长度在 5 到 30 个字符', trigger: 'blur' }
          ],
          newsTwo: [
            { required: true, message: '请再次输入新密码', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      /**
       * 修改自身的密码
       */
      updPassword(){
        if (this.news !== this.newsTwo){
          this.$message.error('新密码两次输入不一致');
          return
        }

        let localStorage = window.localStorage;
        let loginPasswordKey = localStorage.loginPasswordKey

        http.post("/user/updPassword", {
          // 加密密码
          old: desEncrypt(this.formPassword.old, loginPasswordKey),
          news: desEncrypt(this.formPassword.news, loginPasswordKey),
        }).then(response => {
          let res = response.data;
          if (res.status === 'error'){
            // 401是token过期跳转的
            if (res.code !== 401){
              this.$message.error(res.msg);
            }
            return
          }

          this.$message.success("修改密码成功");
          // 关闭弹出框
          this.initPasswordData()
          this.updatePasswordShow = false

        }).catch(error => {
          this.$message.error(error.message);
        })
      },
      /**
       * 初始化密码框
       */
      initPasswordData(){
        this.formPassword.old = null
        this.formPassword.news = null
        this.formPassword.newsTwo = null
      }
    },
    /**
     * 渲染页面后执行的函数
     */
    mounted (){

    }
  }
</script>

<style scoped>

</style>
